package cn.wecloud.im.multiplayervideodemo.live

import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.widget.FrameLayout
import androidx.core.view.isVisible
import cn.wecloud.im.multiplayervideodemo.R
import cn.wecloud.im.multiplayervideodemo.ext.loadAvatar
import io.livekit.android.renderer.TextureViewRenderer
import kotlinx.android.synthetic.main.view_float_surface.view.*

class FloatSurfaceMemberView @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0
) : FrameLayout(context, attrs, defStyleAttr) {

    var isAudioOff: Boolean = false
        private set
    var isVideoOff: Boolean = false
        private set

    init {
        LayoutInflater.from(context).inflate(R.layout.view_float_surface, this)
        ivDisableVideo.isVisible = isVideoOff
        ivDisableVoice.isVisible = isAudioOff
    }

    /**
     * 更新静音状态
     */
    fun setAudioOff(state: Boolean) {
        isAudioOff = state
        ivDisableVoice.isVisible = isAudioOff
    }


    /**
     * 更新视频显示状态
     */
    fun setVideoOff(state: Boolean) {
        isVideoOff = state
        item_surface_layout.isVisible = !isVideoOff
        ivBgAvatar.isVisible = isVideoOff
        ivDisableVideo.isVisible = isVideoOff
    }

    fun getRenderer(): TextureViewRenderer {
        return item_surface_layout
    }

    fun release() {
        item_surface_layout.release()
    }

    fun setIdentity(identity: String?) {
        tvUsername.text = identity
        ivBgAvatar.loadAvatar("")
    }
}